/**
 * Created by isoft-work 5 on 11/16/14.
 */

$(document).ready(function(){
//   render
   renderUserChangePass();
//   event
   bindEventChangePass();
});

function bindEventChangePass() {
   ISS.EVENT.bindEvent('#PasswordForm_send', 'click', function () {
      clearValidate();
      if(validateChangePass()) {
         var phanQuyen = {};
         mapViewToUser(phanQuyen);
         updateUserPass(phanQuyen);
      }
      else {
         var htmlText = '<div id = "message-error" class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Mật khẩu của bạn cập nhật không thành công</div>\
                            </div>';
         ISS.DOM.before('.l-hasSidebar', htmlText);
      }
   });
}

function validateChangePass() {
   var  pass, passRetype, result;
   pass = ISS.DOM.getVal('#PasswordForm_password');
   passRetype = ISS.DOM.getVal('#PasswordForm_password2');

   if(!pass) {
      ISS.DOM.addClass('#PasswordForm_password', 'error');
      result = false;
   }

   if(!passRetype) {
      ISS.DOM.addClass('#PasswordForm_password2', 'error');
      result = false;
   }

   if( pass && passRetype) {
      if(passRetype != pass) {
         ISS.DOM.addClass('#PasswordForm_password2', 'error');
         result = false;
      } else {
         result = true;
      }
   }
   return result;
}

function clearValidate() {
   ISS.DOM.remove('#message-error');
   ISS.DOM.removeClass('#PasswordForm_password', 'error');
   ISS.DOM.removeClass('#PasswordForm_password2', 'error');
}

function updateUserPass(userInfo) {
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/EditAccount/changePass/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(userInfo),
      success: function (data) {
         var responseInfo = data.responseObject;
         updateUserPassComplete(responseInfo);
      }
   });
}

function updateUserPassComplete(responseInfo) {
   if(responseInfo) {
      ISS.WINDOW.location('/jsp/user/user-account.jsp');
   }
   else {
      ISS.DOM.remove('#message-error');
      var htmlText = '<div id = "message-error" class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Tài khoản của bạn cập nhật không thành công</div>\
                            </div>';
      ISS.DOM.before('.l-hasSidebar', htmlText);
   }
}

function mapViewToUser(phanQuyen) {
   var _thongTinThanhVien = {}
   _thongTinThanhVien.maTV = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName'): ISS.COOKIE.getCookie('userName');
   phanQuyen.thongTinThanhVien = _thongTinThanhVien;
   phanQuyen.matKhau = ISS.DOM.getVal('#PasswordForm_password');
}

function renderUserChangePass() {
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   if(!useName) {
      ISS.WINDOW.location('/jsp/user/user-login.jsp');
   }
   else {
         getInfoUser(useName);
   }
}

function getInfoUser(useName) {
   var user = {};
   user.maTV = useName;
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/EditAccount/getInfoUser/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(user),
      success: function (data) {
         var responseInfo = data.responseObject;
         getInfoUserComplete(responseInfo);
      }
   });
}

function getInfoUserComplete(userInfo) {
   ISS.DOM.text('#PasswordForm_email', userInfo.maTV);
   ISS.DOM.text('#PasswordForm_full_name',userInfo.ho + ' ' + userInfo.ten);
   ISS.DOM.text('#PasswordForm_date',ISS.DATE.getFullDate(new Date(userInfo.ngaySinh)));
}
